package com.xxkk.gameCore.vo;

import com.google.protobuf.Message;
import com.xxkk.gameCore.enums.ErrorCode;
import com.xxkk.gameCore.proto.bo.BoProBuf;

/**
 * @author xxkk
 * @version 1.0.0
 * @title BusinessError
 * @description ʕ•ᴥ•ʔ
 * @create 2023/11/27 10:53
 **/
public class BusinessError extends RuntimeException {
    private ErrorCode errorCode;

    public BusinessError(ErrorCode errorCode) {
        this.errorCode = errorCode;
    }
    public BusinessError(){
        super();
    }
    public BusinessError(String message){
        super(message);
        this.errorCode = ErrorCode.normalErrorCode;
        this.errorCode.setMsg(message);
    }

    public ErrorCode getErrorCode() {
        return errorCode;
    }

    public void setErrorCode(ErrorCode errorCode) {
        this.errorCode = errorCode;
    }

    public Message.Builder newBuilder() {// 构建并返回错误码给前端
        BoProBuf.RespErrorProto.Builder builder = BoProBuf.RespErrorProto.newBuilder();
        if (this.errorCode != null) {
            if (this.errorCode.getMsg() != null)
                builder.setMsg(this.errorCode.getMsg());
            builder.setErrorCode(this.errorCode.getCode());
            if (this.errorCode.getErrorType() != null)
                builder.setErrorType(this.errorCode.getErrorType().getType());
        }
        return builder;
    }

}
